package com.tianji.promotion.handler;

import com.tianji.common.constants.MqConstants;
import com.tianji.promotion.domain.dto.UserCouponDTO;
import com.tianji.promotion.service.IUserCouponService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author liuchun
 * @version 1.0
 * @date 2024/10/29 15:45
 */
@Slf4j
@Component
public class PromotionMqHandler {


    @Autowired
    private IUserCouponService userCouponService;


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "coupon.receive.queue",durable = "true"),
            exchange = @Exchange(name = MqConstants.Exchange.RECEIVE_COUPON_EXCHANGE,type = ExchangeTypes.TOPIC),
            key = MqConstants.Key.receive_coupon
    ))
    public void handlePromotion(UserCouponDTO msg) {
        log.info("收到消息:{}",msg);
        //更新已经发放的数量
        //新增用户券
       userCouponService.checkAndCreateUserCoupon(msg);
    }

}
